package com.scoy.honeymei.activity.home;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentIntAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.TravelDetailBean;
import com.scoy.honeymei.dialog.ShareDialog;
import com.scoy.honeymei.dialog.ShowFullDialog;
import com.scoy.honeymei.fragment.home.WebFragment;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.install.GlideImageLoader;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scoy.honeymei.utils.SystemUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TravelDetailActivity$
 * @Description: 旅游详情
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 下午 14:27$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 下午 14:27$
 * @UpdateRemark: 更新内容
 */
public class TravelDetailActivity extends BaseActivity {


    @BindView(R.id.atd_name_tv)
    TextView atdNameTv;
    @BindView(R.id.atd_line)
    View atdLine;
    @BindView(R.id.atd_tv1)
    TextView atdTv1;
    @BindView(R.id.atd_price_tv)
    TextView atdPriceTv;
    @BindView(R.id.atd_content_tv)
    TextView atdContentTv;
    @BindView(R.id.atd_tv2)
    TextView atdTv2;
    @BindView(R.id.atd_type_tv)
    TextView atdTypeTv;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.atd_tab)
    TabLayout atdTab;
    @BindView(R.id.atd_book_tv)
    TextView atdBookTv;
    @BindView(R.id.atd_buynow_tv)
    TextView atdBuynowTv;
    @BindView(R.id.atd_vp)
    ViewPager atdVp;
    @BindView(R.id.atd_banner)
    Banner atdBanner;
    private TravelDetailActivity mContext;
    private Dialog dialogtr;
    private int tid;
    private String fxUrl;
    private String fxName;
    private String fxContent;
    private String fxImage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_detail);
        ButterKnife.bind(this);
        mContext = TravelDetailActivity.this;
        tid = getIntent().getIntExtra("tid", 0);

        initNormal();

        httpTravelDetail(tid);
    }

    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
        signTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_share_gray, 0, 0, 0);

        initTab();

        Uri uri = getIntent().getData();
        if (uri != null) {
            String tttid = uri.getQueryParameter("id");
            try {
                tid = Integer.parseInt(tttid);
            } catch (Exception ignored) {
            }
        }

    }

    @OnClick({R.id.back_iv, R.id.atd_book_tv, R.id.atd_buynow_tv, R.id.sign_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.sign_tv:
                ShareDialog.newInstance(fxImage, fxName, fxContent, fxUrl).show(getSupportFragmentManager(), "");
                break;
            case R.id.atd_book_tv:
                onNotice(1);

                break;
            case R.id.atd_buynow_tv:
                onNotice(2);
                break;
        }
    }

    private void initTab() {
        int[] mTitles = new int[]{R.string.attractindetail, R.string.prebooknotice};
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(WebFragment.newInstance(1, tid));
        fragments.add(WebFragment.newInstance(2, tid));


        TabFragmentIntAdapter tabFragmentAdapter = new TabFragmentIntAdapter(mContext, getSupportFragmentManager(), fragments, mTitles);
        atdTab.setupWithViewPager(atdVp, false);
        atdVp.setAdapter(tabFragmentAdapter);
        atdVp.setCurrentItem(0);
    }


    public void onBook() {
        dialogtr = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_travelbook, null);
        EditText nameEt = view.findViewById(R.id.tb_name_et);
        EditText phoneEt = view.findViewById(R.id.tb_phone_et);
        EditText contentEt = view.findViewById(R.id.tb_content_et);
        TextView sureTv = view.findViewById(R.id.tb_sure_tv);


        dialogtr.setContentView(view);
        dialogtr.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogtr.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogtr.dismiss();
                preckick(nameEt, phoneEt, contentEt);
            }
        });

        dialogtr.show();


    }

    //景点详情
    private void httpTravelDetail(int tid) {
        HttpParams params = new HttpParams();
        params.put("id", tid);
        HpGo.newInstance().HttpGo(this, MyUrl.TRAVEL_DETAIL, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                TravelDetailBean travelDetailBean = new Gson().fromJson(o, TravelDetailBean.class);
                atdNameTv.setText(travelDetailBean.getName());
                atdPriceTv.setText("￥" + travelDetailBean.getPrice());
                atdTypeTv.setText(travelDetailBean.getType_name());

                List<String> images = travelDetailBean.getImages();
                initBanner(atdBanner, images);

                fxUrl = travelDetailBean.getFx_url();
                fxName = travelDetailBean.getFx_title();
                fxContent = travelDetailBean.getFx_miaoshu();
                fxImage = MyUrl.PREFIX_PIC +  travelDetailBean.getFx_image();

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    /**
     * banner图
     */
    public void initBanner(Banner banner, List<String> bannerimglist) {
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(bannerimglist);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(true);
        banner.setDelayTime(4000);
        banner.setOnBannerListener(position -> {
            List<String> list = new ArrayList<>();
            for (String bean : bannerimglist) {
                list.add(MyUrl.PREFIX_PIC + bean);
            }
            ShowFullDialog.newInstance(list).show(getSupportFragmentManager(), position + "");
        });
        banner.start();
    }


    private void preckick(EditText et0, EditText et1, EditText et2) {
        if (TextUtils.isEmpty(et0.getText())) {
            MyUtil.mytoast(mContext, "请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(et1.getText())) {
            MyUtil.mytoast(mContext, "请输入联系电话");
            return;
        }

        if (!CheckUtil.isMobileNO(et1.getText().toString())) {
            MyUtil.mytoast(mContext, "请输入正确的联系电话");
            return;
        }

        if (TextUtils.isEmpty(et2.getText())) {
            MyUtil.mytoast(mContext, "请输入姓名");
            return;
        }

        httpTravelBook(et0.getText().toString(), et1.getText().toString(), et2.getText().toString());

    }

    //预约
    private void httpTravelBook(String st0, String st1, String st2) {
        HttpParams params = new HttpParams();
        params.put("name", st0);
        params.put("tel", st1);
        params.put("detail", st2);
        params.put("attractions_id", tid);
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.TRAVEL_BOOK, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast0(mContext, "预约成功", R.mipmap.ic_sure);

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871:
                break;
        }
    }

    /**
     * 提示
     */
    public void onNotice(int noId) {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_notice, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        TextView notSureTv = view.findViewById(R.id.not_sure_tv);
        WebView notWeb = view.findViewById(R.id.not_web);
        if (noId == 1) {
            notTitleTv.setText("旅游预约须知");
            initWeb(notWeb, MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=1");
        } else {
            notTitleTv.setText("旅游温馨提示");
            initWeb(notWeb, MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=4");
        }


        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //  lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.width = (int) getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        notSureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogNotice.dismiss();
                if (noId == 1) {
                    onBook();
                } else {
                    httpTravelCheckTime();

                }
            }
        });

        dialogNotice.show();
    }

    //预约
    private void httpTravelCheckTime() {
        HttpParams params = new HttpParams();
        params.put("id", tid);
        HpGo.newInstance().HttpGo(this, MyUrl.TRAVEL_CHECK_TIME, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                Intent intent = new Intent(mContext, TravelSettleActivity.class);
                intent.putExtra("trId", tid);
                intent.putExtra("trTime", o);
                startActivityForResult(intent, 0x11);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, /*code + "," +*/ msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }
}
